package com.bwie.server;


import com.bwie.codec.CustomDecoder;
import com.bwie.codec.CustomEncoder;
import com.bwie.util.JT808Const;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;

/**
 * 服务器初始化器
 */
public class TcpServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();

        ByteBuf delimiter = Unpooled.copiedBuffer(new byte[]{JT808Const.PKG_DELIMITER});
        // 参数：最大长度1024字节，剥离分隔符，使用上面创建的分隔符
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(1024, true, delimiter));

        // 添加自定义编解码器
        pipeline.addLast(new CustomDecoder());
        pipeline.addLast(new CustomEncoder());

        // 添加业务处理器
        pipeline.addLast(new TcpServerHandler());
    }
}

